
import { onMounted, reactive, ref, unref } from "vue";
import { post } from '@/utils/https'
export function useHttp<T>(url: string, initialParams: any) {
    const dataList = ref<T[]>([])
    const totals = ref<number>(0)
    const loading = ref<boolean>(false)
    const pageInfo = reactive({
        page: 1,
        pageSize: 10
    })
    
    const loadData = async () => {
        console.log(initialParams,'initialParams')
        try {
            loading.value = true
            const { data: { list, total } } = await post(url, { ...unref(initialParams), ...pageInfo })
            dataList.value = list
            totals.value = total
        } catch (error) {
            console.log(error)
        } finally {
            loading.value = false
        }

    }
    onMounted(() => {
        loadData()
    })
    const handleSizeChange = (size: number) => {
        pageInfo.pageSize = size
        loadData()
    }
    const handleCurrentChange = (page: number) => {
        pageInfo.page = page
        loadData()
    }
    const resetPagination = () => {
        pageInfo.page = 1
        pageInfo.pageSize = 10
        loadData()
    }
    return { dataList, totals, loading, pageInfo, loadData, handleSizeChange, handleCurrentChange, resetPagination }
}